﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

using ZFB.Common;
using ZFB.IServices;

namespace ZFB.WebAPI.Controllers
{
    /// <summary>
    /// 权限
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PermissionController : ControllerBase
    {
        private readonly IPermissionServices permissionServices;

        public PermissionController(IPermissionServices permissionServices)
        {
            this.permissionServices = permissionServices;
        }

        /// <summary>
        /// 获取某个权限
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetById(long id)
        {
            return await permissionServices.GetByIdAsync(id);
        }
        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetAll()
        {
            return await permissionServices.GetAllAsync();
        }
        /// <summary>
        /// 获取名字为name的权限信息GetByName("UserAdd")
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetByName(string name)
        {
            return await permissionServices.GetByNameAsync(name);
        }
        /// <summary>
        /// 获取角色的全部权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetByRoleId(long roleId)
        {
            return await permissionServices.GetByRoleIdAsync(roleId);
        }
        /// <summary>
        /// 给角色roleld 增加权限项id permlds
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="permIds"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddPermIds(long roleId, long[] permIds)
        {
            return await permissionServices.AddPermIdsAsync(roleId, permIds);
        }
        /// <summary>
        /// 更新角色role的权限项:先删除再添加
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="permIds"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdatePermIds(long roleId, long[] permIds)
        {
            return await permissionServices.UpdatePermIds(roleId, permIds);
        }
    }
}
